/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cw.bo;

import java.util.Vector;
import javax.microedition.lcdui.List;
import org.cw.bean.Establishment;
import org.cw.bean.Reviews;
import org.cw.dao.EstablishmentDAO;
import org.cw.dao.ReviewDAO;
import org.cw.view.MReviewer;

/**
 *
 * @author SVN - Team
 */
public class EstablishmentBO {

    private static EstablishmentBO instance;
    public static final int DESC = 1;
    public static final int ASC = 2;
    private EstablishmentDAO estDAO = new EstablishmentDAO();
    private ReviewDAO reDAO = new ReviewDAO();

    public static EstablishmentBO getInstance() {
        if (instance == null) {
            instance = new EstablishmentBO();
        }
        return instance;
    }

    public boolean add(Establishment est) {
        return estDAO.write(est);
    }

    public boolean edit(Establishment est) {
        return false;
    }

    public boolean delete(Establishment est) {
        return false;
    }

    public void list() {
        Vector tmpEst = estDAO.list();
        Vector tmpRe = reDAO.list();
        for (int i = 0; i < tmpEst.size(); i++) {
            Establishment est = (Establishment) tmpEst.elementAt(i);
            for (int j = 0; j < tmpRe.size(); j++) {
                Reviews re = (Reviews) tmpRe.elementAt(j);
                if (est.getId().equals(re.getEstId())) {
                    est.getListReview().addElement(re);
                }
            }
            MReviewer.dataStore.addElement(est);
        }
    }

    public Establishment findById(int idx) {
        return null;
    }

    public Vector sort(Vector listEstablish, int typeSort) {
        Vector v = new Vector();
        for (int i = 0; i < listEstablish.size(); i++) {
            Establishment est = (Establishment) listEstablish.elementAt(i);
            for (int j = 0; j < v.size(); j++) {
                Establishment tmpEst = (Establishment) v.elementAt(j);
                int rs = est.getName().compareTo(tmpEst.getName());
                if (typeSort == DESC) {
                    if (rs < 0) {
                        v.insertElementAt(est, j);
                        break;
                    } else if (rs == 0) {
                        break;
                    }
                } else if (typeSort == ASC) {
                    if (rs > 0) {
                        v.insertElementAt(est, j);
                        break;
                    } else if (rs == 0) {
                        break;
                    }
                }
            }
            if (i >= v.size()) {
                v.addElement(est);
            }
        }
        return v;
    }

    public boolean checkHasId(String id) {
        Establishment tmpEst = null;
        for (int i = 0; i < MReviewer.dataStore.size(); i++) {
            tmpEst = (Establishment) MReviewer.dataStore.elementAt(i);
            if (tmpEst.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    public Establishment getEstablishmentByName(String strName) {
        Establishment tmpEst = null;
        for (int i = 0; i < MReviewer.dataStore.size(); i++) {
            tmpEst = (Establishment) MReviewer.dataStore.elementAt(i);
            if (strName.equals(tmpEst.getName())) {
                return tmpEst;
            }
        }
        return null;
    }

    public void addElementList(Reviews re) {
        for (int i = 0; i < MReviewer.dataStore.size(); i++) {
            Establishment tmpEst = (Establishment) MReviewer.dataStore.elementAt(i);
            if (re.getEstId().equals(tmpEst.getId())) {
                MReviewer.dataStore.removeElementAt(i);
                Vector lReview = tmpEst.getListReview();
                if (lReview.isEmpty()) {
                    lReview = new Vector();
                }
                lReview.addElement(re);
                tmpEst.setListReview(lReview);
                MReviewer.dataStore.addElement(tmpEst);
                break;
            }
        }
    }

    public void EditReviewInEstablishment(Establishment est) {
        for (int i = 0; i < MReviewer.dataStore.size(); i++) {
            Establishment tmpEst = (Establishment) MReviewer.dataStore.elementAt(i);
            if (tmpEst.getId().equals(est.getId())) {
                MReviewer.dataStore.setElementAt(est, i);
                break;
            }
        }
    }

    public void initData() {
        System.out.println("init data");
        Establishment e, e1, e2, e3, e4, e5;
        e = new Establishment();
        e.setId("mp9999");
        e.setName("The Hard Rock Cafe");
        e.setType("Café");
        e.setTypeOfServices("tapas");
        e.setLocation("32 - hoan kiem - ha noi");
        add(e);
        e1 = new Establishment();
        e1.setId("mp008");
        e1.setName("Funky Buddha");
        e1.setType("Bar");
        e1.setTypeOfServices("Gin-tonic");
        e1.setLocation("So 2 Ta Hien, Ha Noi");
        add(e1);
        e2 = new Establishment();
        e2.setId("mp007");
        e2.setName("DRAGON FLY");
        e2.setType("Bar");
        e2.setTypeOfServices("Cocktail");
        e2.setLocation("15 Hang Buom, Ha Noi");
        add(e2);
        e3 = new Establishment();
        e3.setId("mp006");
        e3.setName("Siam Corner");
        e3.setType("Restaurant");
        e3.setTypeOfServices("fish");
        e3.setLocation("33 Tay Ho, Ha Noi");
        add(e3);
        e4 = new Establishment();
        e4.setId("mp005");
        e4.setName("Indochine Restaurant");
        e4.setType("Restaurant");
        e4.setTypeOfServices("sparkling");
        e4.setLocation("16 Pham Ngu Lao, Ha Noi");
        add(e4);
        e5 = new Establishment();
        e5.setId("mp005");
        e5.setName("Lollybooks Café");
        e5.setType("Café");
        e5.setTypeOfServices("Café");
        e5.setLocation("So 18, ngo 131, Thai Ha, Ha Noi");
        add(e5);
    }
}
